Skip to content

Block Hooks API: Add Templates Controller filter to avoid triggering wp_update_post#6225

Closed
tjcafferkey wants to merge 17 commits into
WordPress:trunkfrom
tjcafferkey:update/templates-controller-prepare-database-item-filter
Closed

Block Hooks API: Add Templates Controller filter to avoid triggering wp_update_post#6225
tjcafferkey wants to merge 17 commits into
WordPress:trunkfrom
tjcafferkey:update/templates-controller-prepare-database-item-filter

Conversation

@tjcafferkey

@tjcafferkey tjcafferkey commented Mar 5, 2024

Copy link
Copy Markdown

This change is inspired by this PR WordPress/gutenberg#59561 since they are suffering from the same problem.

By creating a rest_pre_insert_{$this->post_type} filter in theWP_REST_Templates_Controller rather than using the rest_insert_{$this->post_type} action. This avoids calling wp_update_post twice, which was the original reason for the issue, as it removed the backslash from the already-encoded entity. I suspect wp_update_post was also creating additional revisions which was mentioned in the linked Trac ticket.

By creating this filter it also creates consistency with the WP_REST_Posts_Controller which has the same filter.

Trac ticket: https://core.trac.wordpress.org/ticket/60671


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

github-actions Bot commented Mar 5, 2024

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment thread src/wp-includes/block-template-utils.php Outdated
@tjcafferkey tjcafferkey marked this pull request as ready for review March 5, 2024 14:25
@github-actions

github-actions Bot commented Mar 5, 2024

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props tomjcafferkey, bernhard-reiter, gziolo, swissspidy, karolmanijak.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@tjcafferkey tjcafferkey changed the title Add filter to templates REST API controller to avoid using wp_update_… Block Hooks API: Add Templates Controller filter to avoid triggering wp_update_post Mar 5, 2024
Comment thread src/wp-includes/block-template-utils.php Outdated
Comment thread src/wp-includes/block-template-utils.php Outdated
@ockham ockham force-pushed the update/templates-controller-prepare-database-item-filter branch from 376ebdf to 0e65613 Compare March 5, 2024 15:16
Comment thread src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php Outdated
@ockham ockham requested a review from gziolo March 5, 2024 15:30
@ockham

ockham commented Mar 5, 2024

Copy link
Copy Markdown
Contributor

This should be ready for review 😊

Comment thread src/wp-includes/block-template-utils.php
@gziolo

gziolo commented Mar 6, 2024

Copy link
Copy Markdown
Member

By creating this filter it also creates consistency with the WP_REST_Posts_Controller which has the same filter.

That makes sense to me. Avoiding two database operations should be one of the goals of this fix. See my #6225 (comment), where I'm unsure if it will work as intended for the logic that injects blocks.

Comment thread src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php Outdated
Comment thread src/wp-includes/block-template-utils.php
Comment thread src/wp-includes/block-template-utils.php

@swissspidy swissspidy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there test coverage? Can we add tests.

…troller.php

Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>

@kmanijak kmanijak left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the PR against the original issue and it works just fine! Thanks @tjcafferkey for picking it up 🚀

Comment thread src/wp-includes/block-template-utils.php
@ockham

ockham commented Mar 6, 2024

Copy link
Copy Markdown
Contributor

Is there test coverage? Can we add tests.

I'll add some 👍

@ockham ockham force-pushed the update/templates-controller-prepare-database-item-filter branch from fb29d99 to 9bd9fcb Compare March 6, 2024 12:04
@ockham

ockham commented Mar 6, 2024

Copy link
Copy Markdown
Contributor

@swissspidy I've added some tests, both for the filter in isolation, and when used inside the controller. LMK if this looks good to you, or if you'd like anything else covered 🙂

Comment thread tests/phpunit/tests/block-template-utils.php

@gziolo gziolo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍🏻

@ockham

ockham commented Mar 7, 2024

Copy link
Copy Markdown
Contributor

Committed to Core in https://core.trac.wordpress.org/changeset/57790.

@ockham

ockham commented Mar 11, 2024

Copy link
Copy Markdown
Contributor

Backported to the 6.5 branch in https://core.trac.wordpress.org/changeset/57802.

pento pushed a commit that referenced this pull request Oct 15, 2024
Bootstrap block bindings sources earlier in the process through an inline script to ensure they are available when developers want to extend them in the client. Following the same pattern other APIs like registering block types are doing.

Props santosguillamot, cbravobernal, gziolo.
Fixes #6225.



git-svn-id: https://develop.svn.wordpress.org/trunk@59238 602fd350-edb4-49c9-b593-d223f7449a82
aslamdoctor pushed a commit to aslamdoctor/wordpress-develop that referenced this pull request Dec 28, 2024
Bootstrap block bindings sources earlier in the process through an inline script to ensure they are available when developers want to extend them in the client. Following the same pattern other APIs like registering block types are doing.

Props santosguillamot, cbravobernal, gziolo.
Fixes WordPress#6225.



git-svn-id: https://develop.svn.wordpress.org/trunk@59238 602fd350-edb4-49c9-b593-d223f7449a82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants